﻿using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using TCL.FUNC;

namespace TCL.EO
{
    //CREATE TABLE tblLog ( 
    //    iLogID       INTEGER  PRIMARY KEY AUTOINCREMENT,
    //    sContent     TEXT,
    //    sUrl         TEXT,
    //    sUrlRef      TEXT,
    //    sIP          TEXT,
    //    sDatabase    TEXT,
    //    iType        INTEGER,
    //    dProcessTime NUMERIC,
    //    tTimeBegin   DATETIME,
    //    tTimeEnd     DATETIME,
    //    bStatus      BOOLEAN
    //);

    [Serializable()]
    public class LogEO
    {
        /// <summary>
        /// iLogID: 0
        /// </summary>
        public Int32 iLogID { get; set; }

        /// <summary>
        /// sContent: EXEC procABC
        /// </summary>
        public String sContent { get; set; }

        /// <summary>
        /// sUrl: http://nguyenmanhthang.net/
        /// </summary>
        public String sUrl { get; set; }

        /// <summary>
        /// sUrlRef: http://nguyenmanhthang.net/ref
        /// </summary>
        public String sUrlRef { get; set; }

        /// <summary>
        /// sIP: 103.18.7.211
        /// </summary>
        public String sIP { get; set; }

        /// <summary>
        /// sDatabase: connMSSql
        /// </summary>
        public String sDatabase { get; set; }

        /// <summary>
        /// iType: TCL.FUNC.ConstEx.Log_iType
        /// </summary>
        public Int16 iType { get; set; }

        /// <summary>
        /// dProcessTime: 18 (mili giây)
        /// </summary>
        public Double dProcessTime { get; set; }

        /// <summary>
        /// tTimeBegin: 9/17/2015 5:05:38 PM
        /// </summary>
        public DateTime tTimeBegin { get; set; }

        /// <summary>
        /// tTimeEnd: 12/17/2015 5:05:38 PM
        /// </summary>
        public DateTime tTimeEnd { get; set; }

        /// <summary>
        /// bState: true
        /// </summary>
        public Boolean bStatus { get; set; }

        public LogEO(SqlCommand _cmd, String _sDatabase, Int16 _iType)
        {
            try
            {
                StringBuilder strTemp = new StringBuilder();
                this.iLogID = 0;
                this.sContent = _cmd.CommandText;
                foreach (SqlParameter item in _cmd.Parameters)
                {
                    strTemp.Append(", " + item.Value);
                }
                if (!String.IsNullOrEmpty(strTemp.ToString()) && strTemp.ToString().Length > 1)
                    this.sContent = this.sContent + strTemp.ToString().Substring(1);
                this.sUrl = OtherEx.getInfoRequest(1);
                this.sUrlRef = OtherEx.getInfoRequest(2);
                this.sIP = OtherEx.getInfoRequest(4);
                this.sDatabase = _sDatabase;
                this.iType = _iType;
                this.dProcessTime = 0;
                this.tTimeBegin = DateTime.Now;
                this.tTimeEnd = SqlDateTime.MinValue.Value;
                this.bStatus = false;
            }
            catch
            {
                this.iType = 001;
                (new Logs()).WriteLog_To_DB(this, false);
            }
        }

        public LogEO(String _sContent, Int16 _iType)
        {
            try
            {
                this.iLogID = 0;
                this.sContent = _sContent;
                this.sUrl = OtherEx.getInfoRequest(1);
                this.sUrlRef = OtherEx.getInfoRequest(2);
                this.sIP = OtherEx.getInfoRequest(4);
                this.sDatabase = null;
                this.iType = _iType;
                this.dProcessTime = 0;
                this.tTimeBegin = DateTime.Now;
                this.tTimeEnd = SqlDateTime.MinValue.Value;
                this.bStatus = false;
            }
            catch
            {
                this.iType = 002;
                (new Logs()).WriteLog_To_DB(this, false);
            }
        }

        public LogEO()
        {
        }
    }
}
